/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.ubc.cc.ajax;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;

import com.apps.datastore.AccountInformationDatastore;
import com.apps.datastore.dao.SubscriptionFeedObject;
import com.apps.datastore.dao.SubscriptionInformationObject;
import com.apps.utils.FeedUtils;

public class SubscriptionController extends HttpServlet{
	
	AccountInformationDatastore d = new AccountInformationDatastore();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String action = req.getParameter("action");
		String email = req.getParameter("email");
		Cookie[] cookies= req.getCookies();
		String user = null;
		for(int i = 0; i < cookies.length; i++) {
			if(cookies[i].getName().equals("user"))
				user = cookies[i].getValue();		
		}
		String xmlout = "<body>\n"; 
		xmlout += "\t<response>\n";
		boolean type = false;
		if (user != null) {
			if (action.equals("add")) {
				boolean success = d.addSubscription(user, email);
				if(success) {
					xmlout += StringEscapeUtils.escapeXml("Invitation has been sent to <b>"+email+"</b>!");
					type = true;
				}
				else
					xmlout +="Unable to send friend request.";
			}
			else if (action.equals("accept")){
				boolean success = d.activateSubscription(user, email);
				if(success) {
					xmlout +=StringEscapeUtils.escapeXml("You have accepted <b>"+email+"</b> as a friend.");
					type = true;
				}
				else
					xmlout +=StringEscapeUtils.escapeXml("Failed to accept <b>"+email+"</b> as a friend.");
			}
			else if (action.equals("decline")){
				boolean success = d.declineSubscription(user, email);
				if(success) {
					xmlout +=StringEscapeUtils.escapeXml("You have declined <b>"+email+"</b> as a friend.");
					type = true;
				}
				else
					xmlout +=StringEscapeUtils.escapeXml("Failed to decline <b>"+email+"</b> as a friend.");
			}
			else if (action.equals("publish")){
				String msg = req.getParameter("msg");
				msg = URLDecoder.decode(msg,"UTF-8");
				boolean success = d.publishFeed(user, msg);
				if(success){
					xmlout +="Post successful!";
					type = true;
				}
				else
					xmlout +="Post failed.";
			}
			else if (action.equals("subscriptions")){
				String sub_type = req.getParameter("type");
				if(sub_type.equalsIgnoreCase(AccountInformationDatastore.SUBSCRIPTION_STATUS_PENDING))
					sub_type = AccountInformationDatastore.SUBSCRIPTION_STATUS_PENDING;
				else if(sub_type.equalsIgnoreCase(AccountInformationDatastore.SUBSCRIPTION_STATUS_WAITING))
					sub_type = AccountInformationDatastore.SUBSCRIPTION_STATUS_WAITING;
				else if(sub_type.equalsIgnoreCase(AccountInformationDatastore.SUBSCRIPTION_STATUS_ACTIVE))
					sub_type = AccountInformationDatastore.SUBSCRIPTION_STATUS_ACTIVE;
				List<SubscriptionInformationObject> sub_p_list = d.getSubscriptions(user, sub_type);
				for(Iterator<SubscriptionInformationObject> i = sub_p_list.iterator(); i.hasNext();){
					SubscriptionInformationObject s = i.next();
					xmlout += "\t<subscription>\n";
					xmlout += "\t\t<email>"+s.getEmail()+"</email>\n";
					xmlout += "\t\t<status>"+s.getStatus()+"</status>\n";
					xmlout += "\t</subscription>\n";
				}
				if(!sub_p_list.isEmpty())
					type = true;
			}
		}
		else {
	    	xmlout += "Error authenticating user.";
		}
		xmlout += "</response>\n";
		xmlout += "\t<type>"+type+"</type>\n";
		xmlout += "</body>";
		try {
			resp.setContentType("text/xml; charset=UTF-8");
			resp.getWriter().write(xmlout);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
